<template>
  <div class="app-container">

    <!-- 搜索区 -->
    <el-form :model="queryParam" ref="queryForm" size="small" :inline="true">
      <el-form-item label="字典名称" prop="dictName">
        <el-input v-model="queryParam.dictName" placeholder="请输入字典名称" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="字典类型" prop="dictType">
        <el-input v-model="queryParam.dictType" placeholder="字典类型" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>


      <el-form-item label="状态" prop="status">
         <el-select v-model="queryParam.status" placeholder="请选择"  clearable>
            <el-option
              v-for="item in dicts.sys_normal_disable"
              :key="item.id"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
      </el-form-item>

      <el-form-item label="创建时间"  >
           <el-date-picker
                v-model="createTimeValue"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>


    <!--功能区-->
        <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleAdd()"
                >新增</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="success"
                  plain
                  icon="el-icon-edit"
                  size="mini"
                  :disabled="single"
                  @click="handleUpdate()"
                >修改</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  size="mini"
                  :disabled="multiple"
                  @click="handleDelete()"
                >删除</el-button>
              </el-col>

              <el-col :span="1.5">
                <el-button
                  type="info"
                  plain
                  icon="el-icon-download"
                  size="mini"
                  @click="handleExport()"
                >导出数据</el-button>
              </el-col>

              <el-col :span="1.5">
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-refresh"
                  size="mini"
                  @click="handleRefreshCache()"
                >刷新缓存</el-button>
              </el-col>

        </el-row>


    <el-table :data="tableData.rows" border style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="id" label="编号" width="80" />
      <el-table-column prop="dictName" label="字典名称"   />
      <el-table-column prop="dictType" label="字典类型">
        <template slot-scope="scope">
             <el-link type="primary" @click="jumpUrl(scope.row)">{{scope.row.dictType}}</el-link>
        </template>
      </el-table-column>

      <el-table-column label="状态" width="180">
        <template slot-scope="scope">
          <DictTag :dataArray="dicts.sys_normal_disable" :value="scope.row.status"></DictTag>
        </template>
      </el-table-column>

      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column prop="remark" label="备注" width="180"  />

      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template slot-scope="scope">
         <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" >修改</el-button>
         <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条 -->
    <el-pagination v-if="tableData.total>0" :current-page="queryParam.page" :page-sizes="[1, 5, 10, 20,30,50,100]"
      :page-size="queryParam.size" layout="total, sizes, prev, pager, next, jumper" :total="tableData.total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />


   <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false" :before-close="cancelForm">
        <el-form ref="form" :model="form"  label-width="80px">

          <el-form-item label="字典名称" prop="dictName" :rules="[  { required: true, message: '该值不能为空', trigger: 'blur' }]">
            <el-input v-model="form.dictName" placeholder="请输入字典名称" />
          </el-form-item>

          <el-form-item label="字典类型" prop="dictType" :rules="[  { required: true, message: '该值不能为空', trigger: 'blur' }]">
            <el-input v-model="form.dictType" placeholder="请输入字典类型" />
          </el-form-item>

          <el-form-item label="状态" prop="status" :rules="[  { required: true, message: '该值不能为空', trigger: 'blur' }]">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in dicts.sys_normal_disable"
                :key="dict.dictId"
                :label="dict.dictValue"
              >{{dict.dictLabel}}</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancelForm">取 消</el-button>
        </div>
      </el-dialog>

  </div>
</template>

<script>
  import DictMixin from '@/mixins/DictMixin.js'
  import PageMixin from '@/mixins/PageMixin.js'
  import AddOrEditMixin from '@/mixins/AddOrEditMixin.js'
  import {dataApi, addApi, editApi, findByIdApi,deleteByIdApi, freshCache } from '@/api/dicttype.js'
  export default{
     dicts: ['sys_normal_disable'],
     mixins: [DictMixin, PageMixin,AddOrEditMixin],
     data(){
       return {
         // 查询函数
         fetchDataApi: dataApi,
         // 新增保存函数
         addApi: addApi,
          // 修改保存函数
         editApi: editApi,
          // 根据主键查询函数
         findByIdApi: findByIdApi,
         // 逻辑删除
         deleteByIdApi:deleteByIdApi,
         primaryKey: 'id'
       }
     },
     methods: {
       // 重置表单
       initFormValue(){
         this.form= {
           status: '0'
         }
       },
       //跳转到明细页面
       jumpUrl(row){
         this.$router.push('/dict/data/'+ row.id)
       },
       // 刷新缓存
       handleRefreshCache(){

         this.$confirm('确定要刷新redis的缓存数据吗?', '提示', {
               confirmButtonText: '确定',
               cancelButtonText: '取消',
               type: 'warning'
             }).then(() => {

               this.executeRefreshCache()

             }).catch(() => {

             });
       },
       executeRefreshCache(){
          const loading = this.$loading({
             lock: true,
             text: '正在执行操作，请稍待...',
             spinner: 'el-icon-loading',
             background: 'rgba(0, 0, 0, 0.7)'
           });

          freshCache().then(resp=> {
            //关闭动画
            loading.close();
            this.$message({  type: 'success',  message: '操作成功'})
          }).cache(e=> {
            //关闭动画
            loading.close();
          })

       },
       // 导出数据到excel中
       handleExport(){
         this.download('/api/dicttype/download',this.queryParam, `${new Date().getTime()}_dicttype.xlsx`)
       }


     },
     created(){
       this.initFormValue()
     }
  }
</script>

<style>
</style>
